home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_073 / add / until.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  117 lines

  1. /*
  2.  *  This is a short program which will wait until a particular window
  3.  *  is opened, or until a certain amount of time has passed.
  4.  *
  5.  *  Specify -m at the end to wait until the window has finished setting
  6.  *  its menu structure.
  7.  *
  8.  *  Usage : until <window> <seconds> [ <-m> ]
  9.  *
  10.  *  NB: Window name is case sensitive; for multi-word names, only the first
  11.  *      is checked, so if name is "My Window" use "My" when invoking "until".
  12.  */
  13.  
  14. /*
  15.  *  Use of this program? Anytime you run one application that opens windows,
  16.  *  and then want to start another, but not until the first has opened its
  17.  *  window (and optionally constructed a menulist). Could also be used to
  18.  *  reduce disk grinding (hey, this is a neat idea I just thought of!):
  19.  *
  20.  *      run Scribble!       ;fine and dandy
  21.  *      until Scribble! 20  ;wait till loaded, or 20 secs if load fails
  22.  *      run Pagesetter      ;I'll imagine I have lots of ram
  23.  *
  24.  *  This works well as a companion to ADD, so you can construct script files
  25.  *  that run a program like Scribble or Pagesetter, wait until they have
  26.  *  finished loading, and then do an addbuffers and add some keyboard
  27.  *  shortcuts:
  28.  *
  29.  *      run df1:vt100       ;start it up
  30.  *      until VT100 20 -m   ;wait until it loads, gets ser: device, and
  31.  *                          ;constructs menu
  32.  *      addbuffers df0: 20
  33.  *      add VT100 1 1 C     ;create keyboard shortcuts
  34.  *      etc.
  35.  *
  36.  *             Copyright ((c)) 1987, John Russell. Unlimited distribution.
  37.  */
  38.  
  39. #include <intuition/intuition.h>
  40.  
  41. struct IntuitionBase *IntuitionBase;  /* actually has a use! */
  42. struct Window *findwindow();
  43.  
  44. main(argc, argv)
  45. int argc;
  46. char *argv[];
  47. {
  48.     struct Window *open;
  49.     int seconds,x;
  50.  
  51.     if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L))) {
  52.         puts("No intuition library.\n");
  53.         exit(0);
  54.     }
  55.  
  56.     if ((argc < 3) || (argc > 4)) {      /* instructions */
  57.         puts("Usage: until <window> <seconds> [ <-m> ]\n");
  58.         CloseLibrary(IntuitionBase);
  59.         exit(0);
  60.     }
  61.  
  62.     seconds = atoi(argv[2]);
  63.  
  64.     for (x=0; x<2*seconds; x++) {   /* check every 1/2 second */
  65.         if (open = findwindow(argv[1])) {   /* if window open yet */
  66.  
  67.             /* now is menu bar set (or doesn't user care)? */
  68.             if (open->MenuStrip || (argc==3) || compare(argv[3],"-m")) {
  69.                 goto end;
  70.             }
  71.         }
  72.         Delay(25);  /* half a second or so */
  73.     }
  74.  
  75.     puts("Timeout - window did not open.");
  76.     end:
  77.     CloseLibrary(IntuitionBase);
  78. }
  79.  
  80. struct Window *findwindow(title)
  81. char *title;
  82. {
  83.     struct Screen *Screen;
  84.     struct Window *Window;
  85.  
  86.     Forbid();   /* just to be safe, disable interrupts */
  87.  
  88.     /* now loop through all screens/windows */
  89.     for (Screen=IntuitionBase->FirstScreen; Screen; Screen=Screen->NextScreen) {
  90.  
  91.         for (Window=Screen->FirstWindow; Window; Window=Window->NextWindow) {
  92.  
  93.             /* if found, return window pointer so we can check menu status */
  94.             if (!compare(Window->Title,title)) {
  95.                 Permit();   /* what happens if I don't do this, hm? */
  96.                 return(Window);
  97.             }
  98.         }
  99.     }
  100.     Permit();
  101.     return(0L);
  102. }
  103.  
  104. compare(string1,string2)  /* compare strings that end with nulls or spaces */
  105. char *string1,*string2;
  106. {
  107.     while ((*string1 != '\0') && (*string1 != ' ')) {
  108.         if (*string1 != *string2)
  109.             return(1);
  110.         string1++;
  111.         string2++;
  112.     }
  113.     return(0);  /* return reverse of intuitive value, as strcmp does */
  114. }
  115.  
  116.  
  117.